import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/widgets/keep_alive.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';
import 'widget/item.dart';

class ArticleMoreTabListPage extends StatefulWidget {
  const ArticleMoreTabListPage({Key? key}) : super(key: key);

  @override
  State<ArticleMoreTabListPage> createState() => _ArticleMoreTabListPageState();
}

class _ArticleMoreTabListPageState extends State<ArticleMoreTabListPage> {
  final controller = Get.find<ArticleMoreTabListController>();
  final state = Get.find<ArticleMoreTabListController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: TDNavBar(
          backgroundColor: Colors.transparent,
          title: '文章推荐',
        ),
        body: _buildBody(),
      ),
    );
  }

  _buildBody() {
    return Obx(() => state.firstInit
        ? const Center(child: CircularProgressIndicator())
        : Column(
      crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TDTabBar(
                tabs: [
                  for (var item in state.tabs)
                    TDTab(
                      text: item.label,
                    )
                ],
                controller: controller.tabController,
                backgroundColor: Colors.transparent,
                showIndicator: true,
                isScrollable: true,
              ),
              Expanded(
                  child: TabBarView(
                    controller: controller.tabController,
                children: [
                  for (var i = 0; i < state.tabs.length; i++)
                    _buildPage(state.tabs[i].value, i)
                ],
              ))
            ],
          ));
  }

  _buildPage(categoryId, index) {
    return KeepAliveWrapper(
      child: MoreTabItemView(
        categoryId: categoryId,
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<ArticleMoreTabListController>();
    super.dispose();
  }
}
